import * as api from '../api/api';
import request from '../utils/request';
import { pageSize } from '../utils/common';

export default {
    namespace: 'complain',
    state: {
        complain: {},
        complains: { totalCount: 0, items: [] },
        complainsSearchs: { pageIndex: 1, pageSize, keywords: "", status: "0",  complainType:""},
    },
    effects: {
        /**
         * 获取投诉列表
         * @param param0 
         * @param param1
         */
        *getComplains({ payload }, { call, put }) {
            const { keywords, status, pageIndex, complainType, pageSize } = payload;
            const formKeywords = keywords, formStatus = status, formSkipCount = (pageIndex - 1) * pageSize, formMaxResultCount = pageSize, formComplainType = complainType;
            const complains: api.AbpApplicationServicesDtoPagedResultDtoSingleUserMallComplainsVOComplainViewVO =
                yield request(api.ManagecomplainManageApiFetchParamCreactor.managecomplainManageGetComplainsBySearchAsync
                    ({
                        formKeywords, formStatus, formComplainType, formSkipCount, formMaxResultCount
                    }))
            yield put({ type: 'save', payload: { complains: complains, complainsSearchs: { keywords, pageIndex, pageSize, status, complainType } } });
        },
        /**
         * 获取投诉详情
         * @param param0 
         * @param param1 
         */
        *getComplain({ payload }, { call, put }) {
            const formId = payload.id;
            const complain: api.SingleUserMallComplainsVOComplainViewVO =
                yield request(api.ManagecomplainManageApiFetchParamCreactor.managecomplainManageGetComplainAsync({
                    formId,
                }))
            yield put({ type: 'save', payload: { complain: complain } });
        },
        /**
        * 删除投诉
        * @param param0 
        * @param param1 
        */
        *remove({ payload }, { call, put, select }) {
            const { form, callback } = payload;
            const { complainsSearchs } = yield select(({ complain }) => complain);
            yield request(api.ManagecomplainManageApiFetchParamCreactor.managecomplainManageRemoveAsync(
                { form }
            ));
            yield put({ type: 'getComplains', payload: { ...complainsSearchs } });
            callback && callback(true);
        },
        /**
        * 投诉审核
        * @param param0 
        * @param param1 
        */
        *verifyComplain({ payload }, { call, put, select }) {
            const { form, callback } = payload;
            const { complainsSearchs } = yield select(({ complain }) => complain);
            yield request(api.ManagecomplainManageApiFetchParamCreactor.managecomplainManageVerifyComplainAsync(
                { form }
            ));
            yield put({ type: 'getComplains', payload: { ...complainsSearchs } });
            callback && callback(true);
        },
    },
    reducers: {
        save(state, action) {
            return { ...state, ...action.payload };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, search }) => {
                switch (pathname) {
                    case '/complain/complains':
                        dispatch({ type: 'getComplains', payload: { pageIndex: 1, pageSize, keywords: "", status: "0",  complainType:"" }});
                        break;
                }
            });
        },
    },
};